// pages/resultype/ophthalmicmuscles/ophthalmicmuscles.js
const commonutil = require("../commonutil.js");
const screenresultservice = require("../../../apiservice/screen/screenresultservice.js");
const jsonData = require("../jsondata.js");
const app = getApp();
const regeneratorRuntime = app.globalData.regeneratorRuntime;
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		scid: '',
		customerid: '',
		leadName: '',
		addOrMod:'add',
		//选项集
		eyeObroadObliqueIndex: -1,
		eyeInternalObliqueIndex: -1,
		coverObroadObliqueIndex: -1,
		isCoverObroadIncreaseIndex: -1,
		coverInternalObliqueIndex: -1,
		isCoverInternalIncreaseIndex: -1,
		eyeMovementIndex: -1,
		historyOfStrabismusIndex: -1,
		strabismusIndex: -1,
		notOrAbnormal:jsonData.notOrAbnormal,//无、异常
		haveOrNot:jsonData.haveOrNot,//无、有
		normalOrAbnormal:jsonData.normalOrAbnormal,
		congenitalOrAcquired:jsonData.congenitalOrAcquired,
		noneOrBothOrLeftOrRight:jsonData.noneOrBothOrLeftOrRight,
    schoolId:'',
		/**眼肌及斜视检查**/
		//眼位外斜
		eyeObroadOblique: {
			new_scid: null,
			new_categoryid: 'LB0009',
			new_itemid: 'XM0034',
			new_contentid: 'NR0120',
			new_result: 'SC0000000029',
			new_doctorid: null,
			ownerid: null
		},
		//外斜度数/角度说明
		eooDescription: {
			new_scid: null,
			new_categoryid: 'LB0009',
			new_itemid: 'XM0034',
			new_contentid: 'NR0122',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},
		//眼位内斜
		eyeInternalOblique: {
			new_scid: null,
			new_categoryid: 'LB0009',
			new_itemid: 'XM0035',
			new_contentid: 'NR0117',
			new_result: 'SC0000000029',
			new_doctorid: null,
			ownerid: null
		},
		//内斜度数/角度说明
		eioDescription: {
			new_scid: null,
			new_categoryid: 'LB0009',
			new_itemid: 'XM0035',
			new_contentid: 'NR0119',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},
		//遮盖外斜
		coverObroadOblique: {
			new_scid: null,
			new_categoryid: 'LB0009',
			new_itemid: 'XM0036',
			new_contentid: 'NR0127',
			new_result: 'SC0000000029',
			new_doctorid: null,
			ownerid: null
		},
		//外斜有无增大
		isCoverObroadIncrease: {
			new_scid: null,
			new_categoryid: 'LB0009',
			new_itemid: 'XM0036',
			new_contentid: 'NR0129',
			new_result: 'SC0000000029',
			new_doctorid: null,
			ownerid: null
		},
		//遮盖内斜
		coverInternalOblique: {
			new_scid: null,
			new_categoryid: 'LB0009',
			new_itemid: 'XM0037',
			new_contentid: 'NR0123',
			new_result: 'SC0000000029',
			new_doctorid: null,
			ownerid: null
		},
		//内斜有无增大
		isCoverInternalIncrease: {
			new_scid: null,
			new_categoryid: 'LB0009',
			new_itemid: 'XM0037',
			new_contentid: 'NR0125',
			new_result: 'SC0000000029',
			new_doctorid: null,
			ownerid: null
		},
		//眼球运动
		eyeMovement: {
			new_scid: null,
			new_categoryid: 'LB0009',
			new_itemid: 'XM0038',
			new_contentid: 'NR0114',
			new_result: 'SC0000000013',
			new_doctorid: null,
			ownerid: null
		},
		//眼球运动描述
		eyeMovementDescription: {
			new_scid: null,
			new_categoryid: 'LB0009',
			new_itemid: 'XM0038',
			new_contentid: 'NR0116',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},
		//斜视病史
		historyOfStrabismus: {
			new_scid: null,
			new_categoryid: 'LB0009',
			new_itemid: 'XM0039',
			new_contentid: 'NR0109',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},
		//斜视
		strabismus: {
			new_scid: null,
			new_categoryid: 'LB0009',
			new_itemid: 'XM0040',
			new_contentid: 'NR0111',
			new_result: 'SC0000000029',
			new_doctorid: null,
			ownerid: null
		},
		//斜视类型
		strabismusDescription: {
			new_scid: null,
			new_categoryid: 'LB0009',
			new_itemid: 'XM0040',
			new_contentid: 'NR0113',
			new_result: '',
			new_doctorid: null,
			ownerid: null
		},
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		var options = options;
		if (options) {
			this.setData({
				leadName: options.leadname,
				customerid: options.sccustid,
				scid: options.scid,
				categoryid: options.categoryid
			})
			this.getOphthalmicmusclesResults(options.scid, options.sccustid, options.categoryid);
      if(options.schoolId){
        this.setData({schoolId:options.schoolId});
      }
		}
	},
	//提交表单
	ophthalmicmusclesSubmit: function(e) {
		var createResult = this.getOphthaimicmuscles();
		var that = this;
		console.log(createResult);
		commonutil.createScreenResults(createResult,function(res){
			var idArray = res.result.split('|');
			for(var i = 0;i < idArray.length;i++){
				createResult[i].new_sc_resultId = idArray[i];
			}
			that.setData({
				addOrMod: "mod",
				eyeObroadOblique:	createResult[0],
				eooDescription:	createResult[1],
				eyeInternalOblique:	createResult[2],
				eioDescription:	createResult[3],
				coverObroadOblique:	createResult[4],
				isCoverObroadIncrease:	createResult[5],
				coverInternalOblique:	createResult[6],
				isCoverInternalIncrease:	createResult[7],
				eyeMovement:	createResult[8],
				eyeMovementDescription:	createResult[9],
				historyOfStrabismus:	createResult[10],
				strabismus:	createResult[11],
				strabismusDescription:	createResult[12],
			});
		});
	},
	//更新
	ophthalmicmusclesUpdate:function(){
		var updateResult = this.getOphthaimicmuscles();
		commonutil.updateScreenResults(updateResult);
	},
	//查看报告
	enterDetail: async function(e) {
		var scid = this.data.scid;
		var leadid = this.data.customerid;
		var lead = {};
		var data = await commonutil.getLeadInfo(leadid);
		lead.leadid = data.leadId;
		lead.customername = data.lastName;
		lead.age = data.new_age;
		lead.sex = data.new_gender;
		lead.iphoneno1 = data.new_phone;
		lead.sfz = data.new_citizenid;
		lead.createdon = data.modifiedOn;
    var url = "/pages/print/letter/letter?scid=" + scid;
    if (this.data.schoolId)
      url += "&schoolId=" + this.data.schoolId;
		wx.navigateTo({
			url: url,
			events: {
				// 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
				acceptDataFromOpenedPage: function(data) {
					console.log(data)
				},
				someEvent: function(data) {
					console.log(data)
				}
			},
			success: function(res) {
				// 通过eventChannel向被打开页面传送数据
				res.eventChannel.emit('acceptDataFromOpenerPage', {
					data: lead
				})
			}
		})
	},
	//获取结果集
	getOphthalmicmusclesResults: async function(scid, customerid, categoryid) {
		wx.showLoading({
			title:'加载中...',
			mask:true,
		})
		var success = await screenresultservice.GetScreenResultByCategory(scid, customerid, categoryid);
		var reusltArray = success.data
		if(reusltArray.length==0){
			wx.hideLoading();
			return ;
		}
		this.setData({
			addOrMod:'mod'
		})
		for (let index in reusltArray) {
			var element = reusltArray[index];
			if (element.new_contentid === 'NR0120') {
				this.setData({
					eyeObroadOblique: element
				})
			} else if (element.new_contentid === 'NR0122') {
				this.setData({
					eooDescription: element
				})
			} else if (element.new_contentid === 'NR0117') {
				//debugger;
				this.setData({
					eyeInternalOblique: element
				})
			} else if (element.new_contentid === 'NR0119') {
				this.setData({
					eioDescription: element
				})
			} else if (element.new_contentid === 'NR0127') {
				this.setData({
					coverObroadOblique: element
				})
			}else if (element.new_contentid === 'NR0129') {
				this.setData({
					isCoverObroadIncrease: element
				})
			} else if (element.new_contentid === 'NR0123') {
				this.setData({
					coverInternalOblique: element
				})
			}else if (element.new_contentid === 'NR0125') {
				this.setData({
					isCoverInternalIncrease: element
				})
			} else if (element.new_contentid === 'NR0114') {
				this.setData({
					eyeMovement: element
				})
			}else if (element.new_contentid === 'NR0116') {
				this.setData({
					eyeMovementDescription: element
				})
			} else if (element.new_contentid === 'NR0109') {
				this.setData({
					historyOfStrabismus: element
				})
			}else if (element.new_contentid === 'NR0111') {
				this.setData({
					strabismus: element
				})
			}else if (element.new_contentid === 'NR0113') {
				this.setData({
					strabismusDescription: element
				})
				console.log(element);
			}
		}
		wx.hideLoading();
	},
	/**选项集、文本赋值**/
	onEyeObroadObliqueChange: function(e) {
		var result = 'eyeObroadOblique.new_result';
		this.setData({
			[result]: '',
			eyeObroadObliqueIndex: e.detail.index
		})
	},
	onEooDescriptionChange: function(e) {
		var result = 'eooDescription.new_result';
		this.setData({
			[result]: e.detail,
		})
	},
	onEyeInternalObliqueChange: function(e) {
		var result = 'eyeInternalOblique.new_result';
		this.setData({
			[result]: '',
			eyeInternalObliqueIndex: e.detail.index
		})
	},
	onEioDescriptionChange: function(e) {
		var result = 'eioDescription.new_result';
		this.setData({
			[result]: e.detail,
		})
	},
	onCoverObroadObliqueChange: function(e) {
		this.setData({
			coverObroadObliqueIndex: e.detail.index
		})
	},

	onIsCoverObroadIncreaseChange: function(e) {
		this.setData({
			isCoverObroadIncreaseIndex: e.detail.index
		})
	},
	onCoverInternalObliqueChange: function(e) {
		this.setData({
			coverInternalObliqueIndex: e.detail.index
		})
	},

	onIsCoverInternalIncreaseChange: function(e) {
		this.setData({
			isCoverInternalIncreaseIndex: e.detail.index
		})
	},
	onEyeMovementChange: function(e) {
		var result = 'eyeMovement.new_result';
		this.setData({
			[result]: '',
			eyeMovementIndex: e.detail.index
		})
	},
	onEyeMovementDescriptionChange: function(e) {
		var result = 'eyeMovementDescription.new_result';
		this.setData({
			[result]: e.detail,
		})
	},
	onHistoryOfStrabismusChange: function(e) {
		this.setData({
			historyOfStrabismusIndex: e.detail.index
		})
	},
	onStrabismusChange: function(e) {
		this.setData({
			strabismusIndex: e.detail.index
		})
	},
	onStrabismusDescriptionChange: function(e) {
		var result = 'strabismusDescription.new_result';
		this.setData({
			[result]: e.detail,
		})
	},
	/**获取createResult**/
	getOphthaimicmuscles: function() {
		var createResult = [];
		var scid = this.data.scid;
		var customerid = this.data.customerid;

		var eyeObroadOblique = this.data.eyeObroadOblique;
		var eooDescription = this.data.eooDescription;
		var eyeInternalOblique = this.data.eyeInternalOblique;
		var eioDescription = this.data.eioDescription;
		var coverObroadOblique = this.data.coverObroadOblique;
		var isCoverObroadIncrease = this.data.isCoverObroadIncrease;
		var coverInternalOblique = this.data.coverInternalOblique;
		var isCoverInternalIncrease = this.data.isCoverInternalIncrease;
		var eyeMovement = this.data.eyeMovement;
		var eyeMovementDescription = this.data.eyeMovementDescription;
		var historyOfStrabismus = this.data.historyOfStrabismus;
		var strabismus = this.data.strabismus;
		var strabismusDescription = this.data.strabismusDescription;
		//眼位外斜
		var eyeObroadObliqueIndex = this.data.eyeObroadObliqueIndex;
		var notOrAbnormal = this.data.notOrAbnormal;
		if (eyeObroadObliqueIndex >-1) {
			eyeObroadOblique.new_result = notOrAbnormal[eyeObroadObliqueIndex];
		} 
		//眼位内斜
		var eyeInternalObliqueIndex = this.data.eyeInternalObliqueIndex;
		if (eyeInternalObliqueIndex>-1) {
			eyeInternalOblique.new_result = notOrAbnormal[eyeInternalObliqueIndex];
		}
		//遮盖外斜
		var coverObroadObliqueIndex = this.data.coverObroadObliqueIndex;
		if (coverObroadObliqueIndex >-1) {
			coverObroadOblique.new_result = notOrAbnormal[coverObroadObliqueIndex];
		}
		//外斜有无增大
		var isCoverObroadIncreaseIndex = this.data.isCoverObroadIncreaseIndex;
		var haveOrNot = this.data.haveOrNot;
		if (isCoverObroadIncreaseIndex >-1) {
			isCoverObroadIncrease.new_result = haveOrNot[isCoverObroadIncreaseIndex];
		} 
		//遮盖内斜
		var coverInternalObliqueIndex = this.data.coverInternalObliqueIndex;
		if (coverInternalObliqueIndex>-1) {
			coverInternalOblique.new_result = notOrAbnormal[coverInternalObliqueIndex];
		} 
		//内斜有无增大
		var isCoverInternalIncreaseIndex = this.data.isCoverInternalIncreaseIndex;
		if (isCoverInternalIncreaseIndex >-1) {
			isCoverInternalIncrease.new_result = haveOrNot[isCoverInternalIncreaseIndex];
		}
		//眼球运动
		var eyeMovementIndex = this.data.eyeMovementIndex;
		var normalOrAbnormal = this.data.normalOrAbnormal;
		if (eyeMovementIndex >-1) {
			eyeMovement.new_result = normalOrAbnormal[eyeMovementIndex];
		}
		//斜视病史
		var historyOfStrabismusIndex = this.data.historyOfStrabismusIndex;
		var congenitalOrAcquired = this.data.congenitalOrAcquired;
		if (historyOfStrabismusIndex >-1) {
			historyOfStrabismus.new_result = congenitalOrAcquired[historyOfStrabismusIndex];
		} 
		//斜视
		var strabismusIndex = this.data.strabismusIndex;
		var noneOrBothOrLeftOrRight = this.data.noneOrBothOrLeftOrRight;
		if (strabismusIndex >-1) {
			strabismus.new_result = noneOrBothOrLeftOrRight[strabismusIndex];
		} 

		//把对象放入数组
		createResult.push(eyeObroadOblique);
		if(eyeObroadOblique.new_result=='SC0000000005'&&eooDescription.new_result!=''){
			createResult.push(eooDescription);
		}
		createResult.push(eyeInternalOblique);
		if(eyeInternalOblique.new_result=='SC0000000005'&&eioDescription.new_result!=''){
			createResult.push(eioDescription);
		}	
		createResult.push(coverObroadOblique);
		createResult.push(isCoverObroadIncrease);
		createResult.push(coverInternalOblique);
		createResult.push(isCoverInternalIncrease);
		createResult.push(eyeMovement);
		if(eyeMovement.new_result=='SC0000000005'&&eyeMovementDescription.new_result!=''){
			createResult.push(eyeMovementDescription);
		}
		if(historyOfStrabismus.new_result!=''){
			createResult.push(historyOfStrabismus);
		}		
		createResult.push(strabismus);
		if(strabismusDescription.new_result!=''){
			createResult.push(strabismusDescription);
		}		
		//给各结果的筛查id/联系人id赋值
		createResult.forEach(element => {
			element.new_scid = scid;
			element.new_customerid = customerid;
		});
		return createResult;
	}
})
